package mysql

import "project/pkg/models"

func (m Mysql) OpCheckExistByUsername(username string) (exist bool, info *models.OperationInfo, err error) {
	var count int64
	exist = false
	err = m.db.Table("operator_info").Where("username = ?", username).Count(&count).Error
	if err != nil {
		return
	}
	if count > 0 {
		exist = true
		err = m.db.Table("operator_info").Where("username = ?", username).First(&info).Error
		if err != nil {
			return
		}
	}
	return
}
func (m Mysql) OpUpdateInfoBuID(val *models.OperationInfo) (err error) {
	return m.db.Table("operator_info").Save(&val).Error
}

func (m Mysql) OpSearchByTeacherId(id string, page, size int) (res []*models.OperationInfo, count int64, err error) {
	err = m.db.Table("operator_info").Where("create_id = ?", id).Offset((page - 1) * size).Limit(size).Find(&res).Count(&count).Error
	return
}

func (m Mysql) OpDelete(id string) (err error) {
	err = m.db.Table("operator_info").Where("id = ?", id).Delete(&models.OperationInfo{}).Error
	return
}
